package net.piggy.handler;

import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import net.piggy.core.Mediator;
import net.piggy.domain.RequestFuture;
import net.piggy.domain.Response;

public class PiggyServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        RequestFuture request = JSONObject.parseObject(msg.toString(),RequestFuture.class);
        Response response = Mediator.process(request);
        String body = JSONObject.toJSONString(response);
        ctx.channel().writeAndFlush(body);
    }
}
